package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.CommentLikeApi;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.CommentLike;
import com.tanhua.model.mongo.VideoLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class CommentLikeApiImpl implements CommentLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(CommentLike commentLike) {
        long time = System.currentTimeMillis();
        Query query = new Query(Criteria.where("userId").is(commentLike.getUserId())
                .and("commentId").is(commentLike.getCommentId()));
        CommentLike like = mongoTemplate.findOne(query, CommentLike.class);
        Query commentQuery = new Query(Criteria.where("_id").is(commentLike.getCommentId()));
        if(null == like) {
            commentLike.setCreated(time);
            commentLike.setUpdated(time);
            mongoTemplate.insert(commentLike);

            Update update = new Update();
            update.inc("likeCount", 1);
            FindAndModifyOptions famo = new FindAndModifyOptions();
            famo.returnNew(true);
            Comment comment = mongoTemplate.findAndModify(commentQuery, update, famo, Comment.class);
            return comment.getLikeCount();
        }

        Comment comment = mongoTemplate.findOne(commentQuery, Comment.class);
        return comment.getLikeCount();
    }

    @Override
    public Integer remove(CommentLike commentLike) {
        Query query = new Query(Criteria.where("commentId").is(commentLike.getCommentId())
                .and("userId").is(commentLike.getUserId()));
        mongoTemplate.remove(query, CommentLike.class);
        Query commentQuery = new Query(Criteria.where("_id").is(commentLike.getCommentId()));
        Update update = new Update();
        update.inc("likeCount", -1);
        FindAndModifyOptions famo = new FindAndModifyOptions();
        famo.returnNew(true);
        Comment comment = mongoTemplate.findAndModify(commentQuery, update, famo, Comment.class);
        return comment.getLikeCount();
    }

    /**
     * 保存视频点赞
     *
     * @param commentLike
     */
    @Override
    public Integer saveVideoCommentLike(CommentLike commentLike) {
        long currentTime = System.currentTimeMillis();
        Query query = new Query(Criteria.where("userId").is(commentLike.getUserId())
                .and("commentId").is(commentLike.getCommentId()));
        CommentLike one = mongoTemplate.findOne(query, CommentLike.class);
        if (null != one) {
            Update update = new Update();
            update.set("updated", currentTime);
            mongoTemplate.updateFirst(query, update, CommentLike.class);
            Query commentQuery = new Query(Criteria.where("_id").is(one.getCommentId()));
            VideoLike videoLike = mongoTemplate.findOne(commentQuery, VideoLike.class);
            return videoLike.getLikeCount();
        } else {
            commentLike.setCreated(currentTime);
            commentLike.setUpdated(currentTime);
            mongoTemplate.insert(commentLike);

            //修改视频点赞数
            Query videoLikeQuery = new Query(Criteria.where("_id").is(commentLike.getCommentId()));
            Update update = new Update();
            update.inc("likeCount", 1);
            FindAndModifyOptions famo = new FindAndModifyOptions();
            famo.returnNew(true);
            VideoLike videoLike = mongoTemplate.findAndModify(videoLikeQuery, update, famo, VideoLike.class);
            return videoLike.getLikeCount();
        }
    }

    /**
     * 取消视频评论点赞
     *
     * @param commentLike
     */
    @Override
    public Integer removeVideoCommentLike(CommentLike commentLike) {
        Query query = new Query(Criteria.where("userId").is(commentLike.getUserId())
                .and("commentId").is(commentLike.getCommentId()));
        mongoTemplate.remove(query, CommentLike.class);

        Query videoLikeQuery = new Query(Criteria.where("_id").is(commentLike.getCommentId()));
        Update update = new Update();
        update.inc("commentCount", -1);
        FindAndModifyOptions famo = new FindAndModifyOptions();
        famo.returnNew(true);
        VideoLike videoLike = mongoTemplate.findAndModify(videoLikeQuery, update, famo, VideoLike.class);
        return videoLike.getLikeCount();
    }

    /**
     * 用户点赞评论
     *
     * @param userId
     * @param commentIds
     * @return
     */
    @Override
    public Map<ObjectId, Boolean> getHasLiked(Long userId, List<String> commentIds) {
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("commentId").in(commentIds));
        List<CommentLike> commentLikes = mongoTemplate.find(query, CommentLike.class);
        Map<ObjectId, Boolean> resultMap = new HashMap<>();
        for (CommentLike commentLike : commentLikes) {
            resultMap.put(new ObjectId(commentLike.getCommentId()), true);
        }
        return resultMap;
    }
}
